/*
 * Description: Test application for cdev_eth, a character device driver for NIC (RTL8136)
 * Build: gcc -Wall -g application.c -o application `pkg-config --cflags --libs gtk+-3.0`
 * Note: tested on Ubuntu 11.04 running on Lenovo-Z560 Notebook
 * Author: Vijay Chavan <VijayChavan007@gmail.com>	
*/

#include <gtk/gtk.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>

char device[] = {"/dev/cdev_eth"};

/********************************************************/
/*		Read MAC address			*/
/********************************************************/
static void get_mac (GtkWidget *widget, gpointer   data)
{
  GtkTextBuffer * buffer = data;
  g_print ("%s called\n", __FUNCTION__);

   int fd, nb;
   unsigned char rbuff[32];

   fd = open(device, 0666);
   if (fd <= 0)
   {
     gtk_text_buffer_set_text(buffer, "Error opening device!", 21);
   }
   else
  {
   nb = read(fd, rbuff, 6);
   printf("No. of Bytes Read = %d\n", nb);
   close(fd);
   gchar* address = g_strdup_printf("%02x:%02x:%02x:%02x:%02x:%02x", rbuff[0],rbuff[1],rbuff[2],rbuff[3],rbuff[4],rbuff[5]);
   gtk_text_buffer_set_text(buffer, address, 17);
   g_free(address);
  }
}

/********************************************************/
/*	Read Ethernet link Connection Status		*/
/********************************************************/
static void get_link_status (GtkWidget *widget, gpointer   data)
{
  GtkTextBuffer * buffer = data;
  g_print ("%s called\n", __FUNCTION__);

   int fd, nb;
   char status;

   fd = open(device, 0666);
   if (fd <= 0)
   {
     gtk_text_buffer_set_text(buffer, "Error opening device!", 21);
   }
   else
  {
   nb = ioctl(fd, 0, &status);
   close(fd);
   if(status)
     gtk_text_buffer_set_text(buffer, "Connected", 9);
   else
     gtk_text_buffer_set_text(buffer, "Disconnected", 12);
  }
}

/********************************************************/
/*	main function constructing UI Window		*/
/********************************************************/
int main (int   argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *grid;
  GtkWidget *button;
  GtkWidget *text;

  gtk_init (&argc, &argv);

  /* create a new window, and set its title */
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Application: cdev_eth");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_container_set_border_width (GTK_CONTAINER (window), 10);

  /* Here we construct the container that is going pack our buttons */
  grid = gtk_grid_new ();

  /* Pack the container in the window */
  gtk_container_add (GTK_CONTAINER (window), grid);

  GtkWidget * Label = gtk_label_new("Test Application: cdev_eth");
  gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET(Label), 0, 0, 2, 1);

  text = gtk_text_view_new();
  GtkTextBuffer * buffer = gtk_text_buffer_new(NULL);
  gtk_text_buffer_set_text(buffer, "Select Button", 13);
  gtk_text_view_set_buffer((GtkTextView *)text, buffer);
  gtk_text_view_set_editable((GtkTextView *)text, FALSE);

  /* Place Get MAC Button */
  button = gtk_button_new_with_label ("Get MAC");
  g_signal_connect (button, "clicked", G_CALLBACK (get_mac), buffer);
  gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);

  /* Place Link Status Button */
  button = gtk_button_new_with_label ("Get Link Status");
  g_signal_connect (button, "clicked", G_CALLBACK (get_link_status), buffer);
  gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 1, 1);

  /* Place Quit Button */
  button = gtk_button_new_with_label ("Quit");
  g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
  gtk_grid_attach (GTK_GRID (grid), button, 0, 3, 2, 1);

  /* Place Text View */
  gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET(text), 0, 2, 2, 1);

  GtkWidget * Label2 = gtk_label_new("(c) Vijay Chavan <VijayChavan007@gmail.com>");
  gtk_grid_attach (GTK_GRID (grid), GTK_WIDGET(Label2), 0, 4, 2, 1);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
